package com.service.impl;

import com.common.exception.db.InsertException;
import com.common.exception.db.UpdateException;
import com.common.paging.PagingData;
import com.dao.AdviceDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.dto.in.AddStudentDto;
import com.dto.in.AddTeacherDto;
import com.dto.in.LoginDto;
import com.dto.in.StudentDto;
import com.entity.*;
import com.service.StudentService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Administrator on 2018/10/16 0016.
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private AdviceDao adviceDao;

    @Override
    public List<StudentInfo> findStudentList(StudentDto studentDto) {
        List<StudentInfo> info = studentDao.findStudentList(studentDto);
        List<StudentInfo> newList = new ArrayList<>();
            if (info.size() > 0) {
                for (StudentInfo studentInfo : info) {
                    int a = 0;
                    String[] as = studentInfo.getSubjectId().split(",");
                    List<SubjectInfo> subjectInfos = new ArrayList<>();
                    for (int i = 0; i < as.length; i++) {
                        SubjectInfo subInfo = studentDao.findSubject(as[i]);
                        if (subInfo != null) {
                            subjectInfos.add(subInfo);
                        }
                        if (studentDto.getSubjectId() != 0) {
                            if (Integer.parseInt(as[i]) == studentDto.getSubjectId()) {
                                a = a + 1;
                            }
                        }
                    }
                    studentInfo.setSubjectInfo(subjectInfos);
                    if (a > 0) {
                        newList.add(studentInfo);
                    }
                }
                if (studentDto.getSubjectId() == 0) {
                    return info;
                } else {
                    return newList;
                }
            } else {
                return info;
            }

    }

    @Override
    public StudentInfo findStudentWhereId(Integer userId, Integer studentId) {
        StudentInfo studentInfo = studentDao.findStudentWhereId(userId, studentId);
        String[] as = studentInfo.getSubjectId().split(",");
        List<SubjectInfo> subjectInfos = new ArrayList<>();
        for (int i = 0; i < as.length; i++) {
            SubjectInfo subInfo = studentDao.findSubject(as[i]);
            if (subInfo != null) {
                subjectInfos.add(subInfo);
            }
        }
        studentInfo.setSubjectInfo(subjectInfos);
        return studentInfo;
    }

    @Override
    public List<StudentInfo> findStudentByUserId(Integer userId) {
        return studentDao.findStudentByUserId(userId);
    }

    @Override
    public int findCertification(Integer userId) {
        return studentDao.findCertification(userId);
    }

    @Override
    @Transactional
    public int addStudent(AddStudentDto addStudentDto) {
        int success = studentDao.addStudent(addStudentDto);
        if (success > 0) {
            System.out.println(addStudentDto.getStudentId());
            success = studentDao.addStudentDetail(addStudentDto);
            if (success > 0) {
                return success;
            } else {
                throw new InsertException("新增学生详情失败");
            }
        } else {
            throw new InsertException("新增学生表失败");
        }
    }

    @Override
    public List<StudentInfo> myRelease(Integer userId, Integer limit) {
        List<StudentInfo> list = studentDao.myRelease(userId, limit);
        for (StudentInfo studentInfo : list) {
            int asd = adviceDao.findAppliedCount(userId, studentInfo.getId());
            studentInfo.setApplyPeople(asd);
        }
        return list;
    }

    @Override
    public int setMyRelease(Integer userId, Integer studentId) {
        return studentDao.setMyRelease(userId, studentId);
    }


    @Override
    @Transactional
    public int updateStudent(AddStudentDto addStudentDto) {
        int success = studentDao.updateStudent(addStudentDto);
        if (success > 0) {
            success = studentDao.updateStudentDetail(addStudentDto);
            if (success > 0) {
                return success;
            } else {
                throw new UpdateException("修改失败");
            }
        } else {
            throw new UpdateException("修改失败");
        }

    }

    @Override
    public int updateTeacher(AddTeacherDto addTeacherDto) {
        int success = studentDao.updateTeacher(addTeacherDto);
        if (success > 0) {
            return success;
        } else {
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public int findLimit(Integer userId, Integer type) {
        int success;
        if (type == 1) {  //type 1 为查询发布条数限制
            success = studentDao.findPublishLimit(userId);
            if (success >= 5) {
                return 0;
            } else {
                return 1;
            }
        } else if (type == 2) {  //type 2 为申请限制
            success = studentDao.findApplyLimit(userId);
            if (success >= 5) {
                return 0;
            } else {
                return 1;
            }
        } else if (type == 3) { // type 3 为预约限制
            success = studentDao.findReservationLimit(userId);
            if (success >= 5) {
                return 0;
            } else {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public List<ChangePhoneInfo> findChangePhone(Integer userId, Integer type, Integer limit) {
        if (type == 1) {  //学生userId进来的
            return studentDao.findChangePhoneStudent(userId, limit);
        } else {  //老师userId 进来的
            return studentDao.findChangePhoneTeacher(userId, limit);
        }
    }

    @Override
    public int findIsPhoneTeacher(Integer stdUserId, Integer teaUserId) {
        return studentDao.findIsPhoneTeacher(stdUserId, teaUserId);
    }

    @Override
    public int addParentAndTeacher(Integer userId, Integer stdId, Integer teaUserId, Integer teaId) {
        return studentDao.addParentAndTeacher(userId, stdId, teaUserId, teaId);
    }

    @Override
    public int addParentAndTeacher2(Integer userId, Integer stdId, Integer teaUserId, Integer teaId) {
        return studentDao.addParentAndTeacher2(userId, stdId, teaUserId, teaId);
    }

    @Override
    public int findIsHaveData(Integer stdUserId, Integer teaUserId) {
        return studentDao.findIsHaveData(stdUserId, teaUserId);
    }

    @Override
    public int agreeChangePhone(Integer applyPhoneId) {
        return studentDao.agreeChangePhone(applyPhoneId);
    }

    @Override
    public int deleteChangePhone(Integer applyPhoneId) {
        return studentDao.deleteChangePhone(applyPhoneId);
    }

    @Override
    public PagingData findCertificationList(Integer page, Integer length, String search, Integer type) {
        List<CertificationInfo> list = studentDao.findCertificationList(page, length, search, type);
        int count = studentDao.findCertificationListCount(search, type);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    public int setCertification(Integer id, Integer type) {
        return studentDao.setCertification(id, type);
    }

    @Override
    public Map findUserType() {
        int a = studentDao.findUserType1();
        int b = studentDao.findUserType2();
        Map map = new HashMap();
        map.put("student", a);
        map.put("teacher", b);
        return map;
    }

    @Override
    public Map findStatistics() {
        List<AssociationInfo> a = studentDao.findStatistics1();  //申请
        List<AssociationInfo> b = studentDao.findStatistics2();  //预约
        Calendar c = Calendar.getInstance();
        int year;
        int month;
        int[] asdSQ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int year1;
        int month1;
        int[] asdYY = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int o = 0; o < a.size(); o++) {
            c.setTime(a.get(o).getCreateTime());
            year = c.get(Calendar.YEAR); //MONTH 取月
            if (year == 2018) {
                month = c.get(Calendar.MONTH);
                asdSQ[month] = asdSQ[month] + 1;
            }
        }
        for (int i = 0; i < b.size(); i++) {
            c.setTime(b.get(i).getCreateTime());
            year = c.get(Calendar.YEAR); //MONTH 取月
            if (year == 2018) {
                month = c.get(Calendar.MONTH);
                asdYY[month] = asdYY[month] + 1;
            }
        }
        Map map = new HashMap();
        map.put("SQ", asdSQ);
        map.put("YY", asdYY);
        return map;
    }

    @Override
    public PagingData findUserTypeList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        List<LoginInfo> list = studentDao.findUserList(page, length, search);
        for (LoginInfo loginInfo : list) {
            int num = studentDao.findParentMyRelease(loginInfo.getId());
            if (num == 0) {
                loginInfo.setReleaseStatus("未发布");
            } else {
                loginInfo.setReleaseStatus("已发布");
            }
        }
        int count = studentDao.findUserListCount(search);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    public List<StudentInfo> houduanMyRelease(Integer userId, Integer limit) {
        List<StudentInfo> info = studentDao.houduanMyRelease(userId, limit);
        if (info.size() > 0) {
            for (StudentInfo studentInfo : info) {
                String asd = studentInfo.getSubjectId();
                String[] as = studentInfo.getSubjectId().split(",");
                List<SubjectInfo> subjectInfos = new ArrayList<>();
                for (int i = 0; i < as.length; i++) {
                    SubjectInfo subInfo = studentDao.findSubject(as[i]);
                    if (subInfo != null) {
                        subjectInfos.add(subInfo);
                    }
                }
                studentInfo.setSubjectInfo(subjectInfos);
            }
            return info;
        } else {
            return info;
        }
    }

    public List<Teacher> findAll(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        List<Teacher> list = studentDao.findAll(page, length, search);

        for (int j = 0; j < list.size(); j++) {
            if (list.get(j).getSubjectId() != null) {
                StringBuffer strb = new StringBuffer();

                String[] sub = list.get(j).getSubjectId().split(",");
                for (int i = 0; i < sub.length; i++) {
                    String res = teacherDao.findSubjectName(sub[i]);
                    strb.append(res + ",");
                }
                strb.delete(strb.length() - 1, strb.length());
                list.get(j).setSubjectName(strb.toString());
            }
        }
        return list;
    }

    @Override
    public int findCount(String search) {
        int count = studentDao.findCount(search);
        return count;
    }
}
